package com.cnsugar.facerecgonized;

import javax.imageio.ImageIO;

import java.awt.image.BufferedImage;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;

/**
 * @Author ChenZhangKun
 * @Date 2020/4/16 15:15
 */
@SuppressWarnings("all")
public class FaceSave implements Runnable {
    @Override
    public void run() {
        DataInputStream din = null;
        DataOutputStream dout = null;
        while (true) {
            try {
                synchronized (FaceSave.class) {
                    Socket server = FaceRe.serverSocket.accept();
                    // 获取客户端ip
                    InetAddress inetAddress = server.getInetAddress();
                    String hostAddress = inetAddress.getHostAddress();
                    // 获取客户端端口
                    int port = server.getPort();
                    // 获取人脸图片流
                    InputStream inputStream = server.getInputStream();
                   // System.out.println(inputStream.read(new byte[1024*1024]));
                    // 将输入流转换为图片
                    BufferedImage image = ImageIO.read(ImageIO.createImageInputStream(inputStream));
                    // 将人脸存入集合队列，等待做人脸比对
                    FaceRe.imageList.add(image);
                    // 存入map
                    // key=图片，value=客户端ip+端口号
                    FaceRe.map.put(image,hostAddress+","+port);
                }
                } catch(IOException ex){
                    ex.printStackTrace();
                } finally{
                    try {
                        if (din != null) {
                            din.close();
                        }
                        if (dout != null) {
                            dout.close();
                        }
                    } catch (IOException ex) {
                        ex.printStackTrace();
                    }

                }

        }
    }
}
